<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
 SIP protocol implementation
 <p>
 Simple SIP API example
 <pre>
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

import com.aphrodite.io.sip.address.NameAddress;
import com.aphrodite.io.sip.header.StatusLine;
import com.aphrodite.io.sip.message.Message;
import com.aphrodite.io.sip.message.MessageFactory;
import com.aphrodite.io.sip.message.SipResponses;
import com.aphrodite.io.sip.provider.SipProvider;
import com.aphrodite.io.sip.provider.SipProviderListener;
import com.aphrodite.io.sip.transaction.TransactionClient;
import com.aphrodite.io.sip.transaction.TransactionClientListener;
import com.aphrodite.io.sip.transaction.TransactionServer;



public class SIPTest extends MIDlet implements  CommandListener, SipProviderListener, TransactionClientListener
{
   String host_addr="127.0.0.77";
   int host_port=50777;
   String local_user="u77";  
   <font color="0x03F7F5F">//Remote user Address</font>
   NameAddress remote_user=new NameAddress("sip:alice@127.0.0.2");   
   
   Display display;
   Form f;
   TextField tf_send;
   StringItem si_dialog;
   
   Command exitCommand = new Command("Exit", Command.EXIT, 1);
   Command sendCommand = new Command("Send", Command.ITEM, 1);
   
   boolean isPaused=true;
   Message message=null;
   SipProvider sip_provider=null;
   NameAddress from_url;


   public MicroMA()
   {  display=Display.getDisplay(this);
      f=new Form("Testing SIP");
      <font color="0x03F7F5F">//Message to send </font>
      tf_send=new TextField("Send:", "",30,TextField.ANY);
      si_dialog=new StringItem("Dialog:"," ");
      f.append(tf_send);
      f.append(si_dialog);
      f.addCommand(exitCommand);
      f.addCommand(sendCommand);
      f.setCommandListener(this);
      display.setCurrent(f);
      
      <font color="0x03F7F5F">//Setup SIP provider </font>
      String[] protocols={ SipProvider.PROTO_UDP };
      sip_provider=new SipProvider(host_addr,host_port,protocols,null);
      sip_provider.addSipProviderListener(SipProvider.ANY,this);
      from_url=sip_provider.completeNameAddress(local_user);
      
     
      <font color="0x03F7F5F"> // start the local thread</font>
      Thread t=new Thread(this);
      t.start();
   }


   public void startApp()
   {  isPaused=false;
   }
   
   
   public void pauseApp()
   {  isPaused=true;
   }
   
   
   public void destroyApp(boolean unconditional)
   {
   }


   public void commandAction(Command c, Displayable s)
   {
      if (c==exitCommand)
      {  destroyApp(true);
         notifyDestroyed();
      }
      else
      if (c==sendCommand && !isPaused)
      {  String text=tf_send.getString();
         NameAddress to_url=new NameAddress(remote_user);
         send(to_url,from_url,text);
         tf_send.setString("");
         si_dialog.setText(si_dialog.getText()+"\nsent: "+text);
      }
   }


   public void onReceivedMessage(SipProvider provider, Message msg)
   {
      if (msg.isRequest() && msg.isMessage())
      {  remote_user=msg.getFromHeader().getNameAddress();
         si_dialog.setText(si_dialog.getText()+"\nreceived: "+msg.getBody());
         TransactionServer t=new TransactionServer(sip_provider,msg,null);
         t.respondWith(MessageFactory.createResponse(msg,200,SipResponses.reasonOf(200),null));
      }
   }
  <font color="0x03F7F5F"> // Send the SIP message</font>
   public void send(NameAddress to, NameAddress from, String text)
   { 
      message=MessageFactory.createMessageRequest(sip_provider,to,from,null,"application/text",text);
      TransactionClient t=new TransactionClient(sip_provider,message,this);
      t.request();
   }

   public void onTransProvisionalResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
      
   }
   public void onTransSuccessResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
      
   }
   public void onTransFailureResponse(TransactionClient tc, Message msg)
   {  onTransResponse(msg.getStatusLine());
   }
   public void onTransTimeout(TransactionClient tc)
   {  onTransResponse(null);
   }
   
   private void onTransResponse(StatusLine resp)
   {  String result;
      if (resp==null) result="Timout"; else result=resp.getCode()+" "+resp.getReason();
      si_dialog.setText(si_dialog.getText()+"\n"+result);
   }

}
 </pre>
 </p>
</body>

</html>